<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.kkyx')"
            >
              <a-select allowClear v-model="queryParam.pydw" showSearch @change="yxChange" :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.kcmc')"
            >
              <a-select allowClear showSearch v-model="queryParam.kcbh" @change="kcChange" :filterOption="filterOption">
                <a-spin v-if="kcLoading" slot="notFoundContent" size="small"/>
                <a-select-option v-for="(i, index) in selectMenu.courseList" :key="index" :value="i.kch">{{ i.kch + '-' + i.kcmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.kclb')"
              >
                <a-select :allowClear="true" v-model="queryParam.kclb">
                  <a-select-option v-for="i in TYPE0359" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.kkxn')"
              >
                <a-select :allowClear="true" v-model="queryParam.kkxn">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.kkxq')"
              >
                <a-select :allowClear="true" v-model="queryParam.kkxq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.kknj')"
              >
                <a-select :allowClear="true" v-model="queryParam.kknj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="10" :sm="20">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 15px">{{ $t('button.query') }}</a-button>
              <a-button type="primary" @click="handleExport" style="margin:0 15px">{{ $t('button.export') }}</a-button>
              <a-button style="margin: 0 15px" @click="() => queryParam = {}">{{ $t('button.reset') }}</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? $t('button.packUp') : $t('button.advance') }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>
    
    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        ref="table"
        size="default"
        rowKey="index"
        :columns="columns"
        :data="loadData"
        :alert="{show: false}"
        :pagination="{ pageSize: 10 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="xkztmc" slot-scope="text, record, index">
          {{ record.xkztmc ? record.xkztmc : '未修' }}
        </span>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  name: 'attend-lecture',
  data() {
    return {
      form: this.$form.createForm(this),
      kcLoading: false,
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: this.$t('key.no'),
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: this.$t('key.xh'),
          dataIndex: 'xsxh'
        },
        {
          title: this.$t('key.name'),
          dataIndex: 'xm'
        },
        {
          title: this.$t('key.yx'),
          dataIndex: 'dwmc'
        },
        {
          title: this.$t('key.zy'),
          dataIndex: 'zymc'
        },
        {
          title: this.$t('key.kcmc'),
          dataIndex: 'kcmc'
        },
        {
          title: this.$t('key.xkzt'),
          dataIndex: 'xkztmc',
          scopedSlots: { customRender: 'xkztmc' }
        }
      ],
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.tkxsmdList(params).then(res => {
          console.log(res)
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {}
    }
  },
  components: { STable },
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'], // 学年
      TYPE0072: state => state.app['TYPE0072'], // 学期
      TYPE0359: state => state.app['TYPE0359'], // 课程类别
      TYPE0126: state => state.app['TYPE0126'] // 课程类别
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0359')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$nextTick(() => {
        this.$set(this.queryParam, 'kcbh', '')
        this.form.setFieldsValue({kcbm: ''})
        this.$set(this.selectMenu, 'courseList', [])
        if (code) {
          this.kcLoading = true
          this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
            this.$set(this.selectMenu, 'courseList', res.data)
          }).finally(() => {
            this.kcLoading = false
          })
        } else {
          this.$set(this.selectMenu, 'courseList', [])
        }
      })
    },
    kcChange (kch) {
      this.$nextTick(() => {
        let kc = this.selectMenu.courseList.find(kc => {
          return kc.kch === kch
        })
        this.form.setFieldsValue({
          xs: kc.zxs,
          xf: kc.xf
        })
      })
    },
    handleExport () {
      // this.$api.cultivate.tkxsmdExport(this.queryParam).then(res => {
      //   console.log(res)
      // })
      let params = this.queryParam
      this.$export('/teacherClient/tkxsmdExport', {
        ...params
      })
    }
  }
}
</script>
